<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<html>
<head>
    <title>成绩管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
        }
        .top-bar {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .filter-box {
            display: flex;
            align-items: center;
        }
        .filter-box select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
            width: 200px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            color: white;
        }
        .btn-primary {
            background-color: #1ab394;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        .btn-warning {
            background-color: #f8ac59;
        }
        .btn-warning:hover {
            background-color: #f7a54a;
        }
        .btn-danger {
            background-color: #ed5565;
        }
        .btn-danger:hover {
            background-color: #ec4758;
        }
        .btn-info {
            background-color: #23c6c8;
        }
        .btn-info:hover {
            background-color: #21b9bb;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #e7eaec;
        }
        th {
            background-color: #f5f5f6;
            color: #333;
        }
        .score-input {
            width: 80px;
            padding: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            text-align: center;
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
        }
        .pagination button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ddd;
            background-color: white;
            cursor: pointer;
            border-radius: 3px;
        }
        .pagination button:hover:not(:disabled) {
            background-color: #f5f5f5;
            color: #1ab394;
        }
        .pagination button.active {
            background-color: #1ab394;
            color: white;
            border-color: #1ab394;
        }
        .pagination button:disabled {
            background-color: #f5f5f5;
            cursor: not-allowed;
        }
        .pagination-info {
            margin: 0 15px;
            color: #666;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="top-bar">
            <div>
                <button class="btn btn-primary" onclick="location.href='${pageContext.request.contextPath}/admin/subject/add-score'">
                    <i class="fas fa-plus"></i> 添加成绩
                </button>
            </div>
            <div class="filter-box">
                <select id="subjectFilter" onchange="filterBySubject(this.value)">
                    <option value="">全部学科</option>
                    <c:forEach items="${subjectList}" var="subject">
                        <option value="${subject.subjectId}" ${param.subjectId == subject.subjectId ? 'selected' : ''}>
                            ${subject.subjectName}
                        </option>
                    </c:forEach>
                </select>
            </div>
        </div>

        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>学生姓名</th>
                    <th>学科名称</th>
                    <th>成绩</th>
                    <th>录入时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <c:forEach items="${scoreList}" var="score">
                    <tr>
                        <td>${score.scoreId}</td>
                        <td>${score.studentName}</td>
                        <td>${score.subjectName}</td>
                        <td>
                            <input type="number" class="score-input" value="${score.score}" 
                                   min="0" max="100" step="0.01" 
                                   onchange="updateScore(${score.scoreId}, this.value)">
                        </td>
                        <td><fmt:formatDate value="${score.createTime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
                        <td>
                            <button class="btn btn-danger btn-sm" onclick="deleteScore(${score.scoreId})">
                                <i class="fas fa-trash"></i> 删除
                            </button>
                        </td>
                    </tr>
                </c:forEach>
            </tbody>
        </table>

        <div class="pagination">
            <button onclick="goToPage(${currentPage - 1})" ${currentPage <= 1 ? 'disabled' : ''}>
                <i class="fas fa-chevron-left"></i> 上一页
            </button>
            
            <c:forEach begin="1" end="${totalPages}" var="i">
                <c:choose>
                    <c:when test="${i == currentPage}">
                        <button class="active">${i}</button>
                    </c:when>
                    <c:otherwise>
                        <button onclick="goToPage(${i})">${i}</button>
                    </c:otherwise>
                </c:choose>
            </c:forEach>
            
            <button onclick="goToPage(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
                下一页 <i class="fas fa-chevron-right"></i>
            </button>
            
            <span class="pagination-info">
                共 ${totalCount} 条记录，${totalPages} 页
            </span>
        </div>
    </div>

    <script>
        function filterBySubject(subjectId) {
            location.href = '${pageContext.request.contextPath}/admin/subject/scores' + 
                          (subjectId ? '?subjectId=' + subjectId : '');
        }

        function updateScore(scoreId, score) {
            if (score < 0 || score > 100) {
                alert('成绩必须在0-100之间');
                return;
            }

            fetch('${pageContext.request.contextPath}/admin/subject/score/update', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'scoreId=' + scoreId + '&score=' + score
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('成绩更新成功');
                } else {
                    alert(data.message || '成绩更新失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('成绩更新失败');
            });
        }

        function deleteScore(id) {
            if (confirm('确定要删除这条成绩记录吗？')) {
                fetch('${pageContext.request.contextPath}/admin/subject/score/delete?id=' + id, {
                    method: 'POST'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('删除成功');
                        location.reload();
                    } else {
                        alert(data.message || '删除失败');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败');
                });
            }
        }

        function goToPage(page) {
            const subjectId = document.getElementById('subjectFilter').value;
            const subjectParam = subjectId ? '&subjectId=' + subjectId : '';
            location.href = '${pageContext.request.contextPath}/admin/subject/scores?page=' + page + subjectParam;
        }
    </script>
</body>
</html> 